package com.hjlh.aimedical.controller;

import com.hjlh.aimedical.common.respone.ResultModel;
import com.hjlh.aimedical.entity.Medicalrecord;
import com.hjlh.aimedical.model.UserModel;
import com.hjlh.aimedical.service.MedicalrecordService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * (Medicalrecord)表控制层
 *
 * @author makejava
 * @since 2025-06-29 14:44:33
 */
@RestController
@RequestMapping("/api/medicalrecord")
public class MedicalrecordController {
    /**
     * 服务对象
     */
    @Resource
    private MedicalrecordService medicalrecordService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @RequestMapping("/queryByPage")
    @ResponseBody
    public ResultModel queryByPage(
            @RequestParam("pageSize") int pageSize,
            @RequestParam("page") int page,
            HttpSession httpSession) {
        UserModel userModel = (UserModel) httpSession.getAttribute("userModel");
        if(userModel == null){
            return ResultModel.fail("获取失败",null);
        }
        Map<String,Object> map = this.medicalrecordService.queryByPage(pageSize, page,userModel.getUsercode());
        return ResultModel.success("获取成功",map);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Medicalrecord> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.medicalrecordService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param medicalrecord 实体
     * @return 新增结果
     */
    @PostMapping("add")
    @ResponseBody
    public ResultModel add(@RequestBody Medicalrecord medicalrecord) {
        return medicalrecordService.insert(medicalrecord);
    }

    /**
     * 编辑数据
     *
     * @param medicalrecord 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    @ResponseBody
    public ResultModel edit(@RequestBody Medicalrecord medicalrecord) {
        return this.medicalrecordService.update(medicalrecord);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.medicalrecordService.deleteById(id));
    }

}

